package javaguide.algorithm;

import java.util.Arrays;

public class InsertSort {

    public static void main(String[] args) {
        int[] arr = {3, 5, 4, 7, 9, 1, 2, 6, 8};
        insertSort(arr);
        System.out.println("arr = " + Arrays.toString(arr));
    }

    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int preIndex = i - 1;
            int current=arr[i];
            while (preIndex >= 0 && current < arr[preIndex]) {
                arr[preIndex + 1] = arr[preIndex];
                preIndex -= 1;
            }
            arr[preIndex + 1] = current;
        }
    }
}
